import 'package:provider/provider.dart';
import 'package:xixi/global.dart';
import 'package:flutter/material.dart';
import 'package:xixi/main_manager.dart';
import 'package:xixi/page/detail.dart';
import 'package:xixi/utils.dart';

class FavoritePage extends StatefulWidget {
  FavoritePage({Key key}) : super(key: key);
  @override
  _FavoritePageState createState() => _FavoritePageState();
}

class _FavoritePageState extends State<FavoritePage> {
  @override
  void initState() {
    super.initState();
  }

  List<Widget> renderFavoriteList(_mainManager, allInfo) {
    List<Widget> list = [];
    for (var i = 0; i < allInfo.length; i++) {
      var item = allInfo[i];
      list.add(
        InkWell(
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => DetailPage(
                  id: item['id'].toString(),
                  type: item['key'],
                ),
              ),
            );
          },
          child: Container(
            padding: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  color: Global.fontSecondColor.withOpacity(0.3),
                  width: 0.5,
                ),
              ),
            ),
            child: Row(
              children: [
                Container(
                  width: 55,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                  child: ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                    child: FadeInImage.assetNetwork(
                      placeholder: 'assets/images/placeholder.png',
                      fadeInDuration: const Duration(milliseconds: 0),
                      image: item['img'],
                    ),
                  ),
                ),
                SizedBox(width: 10),
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        item['name'],
                        style: TextStyle(
                          color: Global.primaryColor,
                          fontSize: 12,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      Text(
                        item['intro'],
                        maxLines: 4,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: Global.fontColor,
                          fontSize: 10,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(width: 10),
                InkWell(
                  onTap: () {
                    Utils.showConfirmDialog(
                      context: context,
                      content: '确认取消收藏？',
                      confirmCallback: () {
                        _mainManager.like(item);
                      },
                      showCancelButton: true,
                    );
                  },
                  child: Container(
                    padding: EdgeInsets.all(4),
                    decoration: BoxDecoration(
                      color: Global.primaryColor,
                      borderRadius: BorderRadius.circular(20),
                    ),
                    child: Icon(
                      Icons.delete,
                      color: Global.backgroundColor,
                      size: 20,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return _mainManager.allIchongInfo.length == 0
        ? Scaffold(
            appBar: AppBar(
              title: Text("我的宠物收藏", style: TextStyle(fontSize: 14)),
              elevation: 0,
              backgroundColor: Global.primaryColor,
            ),
            body: Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              color: Global.backgroundColor,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    width: 100,
                    child: Image.asset('assets/images/kong.png'),
                  ),
                  SizedBox(height: 20),
                  Text(
                    '没有收藏任何宠物哦',
                    style: TextStyle(
                      color: Global.fontSecondColor.withOpacity(0.6),
                      fontSize: 14,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
            ),
          )
        : Scaffold(
            appBar: AppBar(
              title: Text("我的宠物收藏", style: TextStyle(fontSize: 14)),
              elevation: 0,
              backgroundColor: Global.primaryColor,
            ),
            body: Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              decoration: BoxDecoration(
                color: Global.backgroundColor,
              ),
              child: SingleChildScrollView(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: renderFavoriteList(
                      _mainManager, _mainManager.allIchongInfo),
                ),
              ),
            ),
          );
  }
}
